function initItems()

gameItems = {}

gameItems["empty"] = {}
gameItems.empty[1] = {itemID = "none", desc = "empty", itemtype = "empty"}

gameItems["melee"] = {}
gameItems.melee[1] = {itemID = "knife", desc = "steel knife", itemtype = "melee", damage = 2, range = 3}
gameItems.melee[2] = {itemID = "katana", desc = "nanofibre katana", itemtype = "melee", damage = 3, range = 3}
gameItems.melee[3] = {itemID = "blade", desc = "monomolecular blade", itemtype = "melee", damage = 4, range = 3}

gameItems["guns"] = {}
gameItems.guns[1] = {itemID = "9mm", desc = "9mm handgun", itemtype = "gun", ammo = 11, maxammo = 11, range = 6, damage = 2}
gameItems.guns[2] = {itemID = "rifle", desc = "flechette rifle", itemtype = "gun", ammo = 8, maxammo = 8, range = 10, damage = 3}
gameItems.guns[3] = {itemID = "shotgun", desc = "pulse shotgun", itemtype = "gun", ammo = 10, maxammo = 10, range = 5, damage = 4}

gameItems["utils"] = {}
gameItems.utils[1] = {itemID = "bandage", desc = "small autobandage", itemtype = "bandage", health = 50}
gameItems.utils[2] = {itemID = "heatpod", desc = "red heating module", itemtype = "heatpod", heat = 6, turnsLeft = 9, active = false}
gameItems.utils[3] = {itemID = "medigel", desc = "blue medical module", itemtype = "medigel", health = 15, turnsLeft = 4, active = false}
gameItems.utils[4] = {itemID = "silencer", desc = "grey silencer module", itemtype = "silencer", shotsLeft = 4, active = false}
gameItems.utils[5] = {itemID = "ioncharge", desc = "green ion pulse module", itemtype = "ionpulse"}

--gameItems.utils[4] = {itemID = "scanner", desc = "green scanning module", itemtype = "scanner"}


--gameItems.utils[5] = {itemID = "grenade", desc = "purple explosive module", itemtype = "explosive"}
--gameItems.utils[6] = {itemID = "detonator", desc = "small detonator module", itemtype = "detonator" }

gameItems["armour"] = {}
gameItems.armour[1] = {itemID = "lightarmour", desc = "light ballistic armour module", itemtype = "armour", active = false, armourvalue = 2}
gameItems.armour[2] = {itemID = "mediumarmour", desc = "durasteel medium armour module", itemtype = "armour", active = false, armourvalue = 3}
--gameItems.armour[3] = {itemID = "heavyarmour", desc = "juggernaught heavy armour module", itemtype = "armour", active = false, armourvalue = 4}


-- ammo
gameItems["ammo"] = {}
gameItems.ammo[1] = {itemID = "9mmclip", desc = "handgun clip", itemtype = "clip", targetgun = "9mm", ammo = 11, maxammo = 11}
gameItems.ammo[2] = {itemID = "rifleclip", desc = "box of flechettes", itemtype = "clip", targetgun = "rifle", ammo = 8, maxammo = 8}
gameItems.ammo[3] = {itemID = "shotgunclip", desc = "pulse cartridge", itemtype = "clip", targetgun = "9mm", ammo = 10, maxammo = 10}
--gameItems.ammo[4] = {itemID = "railgunclip", desc = "spike of railgun bolts", itemtype = "clip", targetgun = "9mm", ammo = 4, maxammo = 4}

end

-- Initial player inventory --------------------------------------------- 
function initInventory()

	playerItems[1] = deepcopy(gameItems.guns[1])
	playerItems[1].ammo = 4
	playerItems[2] = deepcopy(gameItems.melee[1])

	playerItems[3] = deepcopy(gameItems.utils[math.random(#gameItems.utils)])
	playerItems[4] = deepcopy(gameItems.utils[math.random(#gameItems.utils)])
	playerItems[5] = deepcopy(gameItems.utils[2])

end


function itemDesc(i,item)

	desc = i..") " .. item.desc

	if (item.itemtype == "gun") then desc = desc .. "  ( " .. item.ammo .."/".. item.maxammo .. " rounds )" end
	if (item.itemtype == "clip") then desc = desc .. "  ( " .. item.ammo .."/".. item.maxammo .. " rounds )" end

	if (item.itemtype == "heatpod" or item.itemtype == "medigel") then
		if (item.active == true) then
			desc = desc .." - ".. item.turnsLeft .. " turns left"
		end
	end

	if (item.itemtype == "silencer" ) then
		if (item.active == true) then
			desc = desc .." - ".. item.shotsLeft .. " shots left"
		end
	end

	return desc
end

-- Heal the player character
function healCharacter(heal)
	characterHealth = characterHealth + heal
	if (characterHealth > 100) then characterHealth = 100 end
end

function heatCharacter(heat)
	characterHeat = characterHeat + heat
	if (characterHeat > 100) then characterHeat = 100 end
end

function getArmourLevel()
	armourItem = hasItem("armour",true)

	if (armourItem ~= nil) then return playerItems[armourItem].armourvalue end

	return 1
end

-- Update inventory
function itemTick()

	for i=1,#playerItems,1 do

		if (playerItems[i].itemtype == "medigel" and playerItems[i].active == true) then 
			healCharacter(playerItems[i].health)
			playerItems[i].turnsLeft = playerItems[i].turnsLeft - 1
		end

		if (playerItems[i].itemtype == "heatpod" and playerItems[i].active == true) then 
			heatCharacter(playerItems[i].heat)
			playerItems[i].turnsLeft = playerItems[i].turnsLeft - 1
		end

		if (playerItems[i].turnsLeft ~= nil and playerItems[i].turnsLeft <= 0) then
			addConsoleMessage("Your " .. playerItems[i].desc .. " burns out, it's charge finished.")
			playerItems[i] = deepcopy(gameItems.empty[1])
		end

		if (playerItems[i].itemtype == "silencer" and playerItems[i].shotsLeft <= 0) then
			addConsoleMessage("Your " .. playerItems[i].desc .. " burns out, it's charge finished.")
			playerItems[i] = deepcopy(gameItems.empty[1])
		end

	end

end

function useItem(i)

	if (playerItems[i].itemtype == "gun") then
	 	setTargetMode(gunCallback,i)
	 	addConsoleMessage("You take aim with your " .. playerItems[i].desc)
	 end

	if (playerItems[i].itemtype == "melee") then
	 	setTargetMode(meleeCallback,i)
	 	addConsoleMessage("You prepare to strike with your " .. playerItems[i].desc)
	 end

	if (playerItems[i].itemtype == "clip") then 

		local target = nil
		if (playerItems[1].itemtype == "gun" and playerItems[i].targetgun == playerItems[1].itemID and playerItems[1].ammo ~= playerItems[1].maxammo ) then target = 1 end
		if (playerItems[2].itemtype == "gun" and playerItems[i].targetgun == playerItems[2].itemID and playerItems[2].ammo ~= playerItems[2].maxammo ) then target = 2 end

		if (target == nil) then
			addConsoleMessage("You can't find a suitable gun for that ammunition")
			return
		end

		playerItems[target].ammo = playerItems[i].ammo
		addConsoleMessage("You reload your " .. playerItems[target].desc .." with the ".. playerItems[i].desc..".","yellow")
		playerItems[i] = deepcopy(gameItems.empty[1])

		gameTick()
	 end

	if (playerItems[i].itemtype == "bandage") then 
		if (characterHealth == 100) then
			addConsoleMessage("You are already in perfect health!")
			return
		end

		playerItems[i] = deepcopy(gameItems.empty[1])
		addConsoleMessage("You apply the autobandage to your wounds","yellow")
		healCharacter(50)
		gameTick()
		return
	end


	if (playerItems[i].itemtype == "medigel" or playerItems[i].itemtype == "heatpod" or playerItems[i].itemtype == "silencer") then 

		if playerItems[i].active == true then
			addConsoleMessage("That module is already activated!")
			return
		end

		addConsoleMessage("You activate the " .. playerItems[i].desc,"yellow")
		playerItems[i].active = true
		gameTick()
		return
	end

	if (playerItems[i].itemtype == "armour") then

		if (playerItems[i].active == true) then
			addConsoleMessage("You remove the " .. playerItems[i].desc .. ".", "yellow")
			characterArmour = 1
			playerItems[i].active = false
			gameTick()
			return
		end


		local currentArmour = hasItem("armour",true)
		if  currentArmour ~=  nil then
			addConsoleMessage("You already have " .. currentArmour.desc .. "equipped as armour!")
			return
		end

		addConsoleMessage("You equip the " .. playerItems[i].desc,"yellow")
		playerItems[i].active = true
		characterArmour = playerItems[i].armourvalue
		gameTick()
		return

	end

	if (playerItems[i].itemtype == "scanner") then
		setTargetMode(scannerCallback,i)
	 	addConsoleMessage("You prepare to throw your " .. playerItems[i].desc)
	end

	if (playerItems[i].itemtype == "ionpulse") then
	 	addConsoleMessage("You activate your ion pulse module!","yellow")
	 	ionPulse(characterLoc[1],characterLoc[2])
	 	playerItems[i] = deepcopy(gameItems.empty[1])
	 	gameTick()
	end

end

function ionPulse(x,y)
	addConsoleMessage("A blast of green light envelops the surrounding area!","green")
	if (getMobOnTile(x,y) ~= nil ) then killMob(getMobOnTile(x,y)) end
	if (getMobOnTile(x+1,y) ~= nil ) then killMob(getMobOnTile(x+1,y)) end
	if (getMobOnTile(x-1,y) ~= nil ) then killMob(getMobOnTile(x-1,y)) end
	if (getMobOnTile(x,y+1) ~= nil ) then killMob(getMobOnTile(x,y+1)) end
	if (getMobOnTile(x,y-1) ~= nil ) then killMob(getMobOnTile(x,y-1)) end
	if (getMobOnTile(x+1,y+1) ~= nil ) then killMob(getMobOnTile(x+1,y+1)) end
	if (getMobOnTile(x-1,y-1) ~= nil ) then killMob(getMobOnTile(x-1,y-1)) end
	if (getMobOnTile(x+1,y-1) ~= nil ) then killMob(getMobOnTile(x+1,y-1)) end
	if (getMobOnTile(x-1,y+1) ~= nil ) then killMob(getMobOnTile(x-1,y+1)) end

end	

function isItemOnGround(x,y)
	for i=1,10,1 do
		if (localTerrain[x][y].items[i].itemtype ~= "empty" ) then return true end
	end

	return false
end

function hasItem(itemtype, active)

	for i=1,#playerItems,1 do
		if (playerItems[i].itemtype == itemtype) then

			if (active == false) then 
				return i
			elseif ( playerItems[i].active ~= nil and playerItems[i].active == true ) then 
				return i
			end
		end
	end

	return nil

end


function attemptPickUp(localID, targetSlot)

	-- not empty
	if (playerItems[targetSlot] == nil or playerItems[targetSlot].itemtype ~= "empty") then
		addConsoleMessage("That slot is not empty!")
		return false
	end

	local type = localTerrain[characterLoc[1]][characterLoc[2]].items[localID].itemtype

	if (type == "gun" or type == "melee") then
		if (targetSlot ~= 1 and targetSlot ~= 2) then
			addConsoleMessage("You have no space for such a large object in your pack. You must wield it!")
			return false
		end
	else
		if (targetSlot == 1 or targetSlot == 2) then
			addConsoleMessage("You cannot wield that!")
			return false
		end
	end

	-- otherwise pick up!
	playerItems[targetSlot] = deepcopy(localTerrain[characterLoc[1]][characterLoc[2]].items[localID])
	localTerrain[characterLoc[1]][characterLoc[2]].items[localID] = deepcopy(gameItems.empty[1])

	-- attempt shift down of items
	for i=10,localID,-1 do
		if (localTerrain[characterLoc[1]][characterLoc[2]].items[i].itemtype ~= "empty" and i ~= localID) then
			localTerrain[characterLoc[1]][characterLoc[2]].items[localID] = deepcopy(localTerrain[characterLoc[1]][characterLoc[2]].items[i])
			localTerrain[characterLoc[1]][characterLoc[2]].items[i] = deepcopy(gameItems.empty[1])
			break
		end
	end

	addConsoleMessage("You pick up the " .. playerItems[targetSlot].desc)

	return true

end

function attemptDrop(targetSlot)
	if (playerItems[targetSlot].itemtype == "empty") then
		addConsoleMessage("You don't have anything in that slot!")
		return false
	end

	-- active item
	if (playerItems[targetSlot].active ~= nil and playerItems[targetSlot].active == true) then 
		addConsoleMessage("You cannot drop an activated module!")
		return
	end

	-- find an available slot
	for i=1,10,1 do
		if (localTerrain[characterLoc[1]][characterLoc[2]].items[i].itemtype == "empty") then

			addConsoleMessage("You drop your ".. playerItems[targetSlot].desc)

			localTerrain[characterLoc[1]][characterLoc[2]].items[i] = deepcopy(playerItems[targetSlot])
			playerItems[targetSlot] = deepcopy(gameItems.empty[1])

			return true
		end
	end

	addConsoleMessage("There is no space on the ground for your ".. playerItems[targetSlot].desc.."!")

	return false
end


function dropItem(targetItem, targetLoc)

	-- find an available slot
	for i=1,10,1 do
		if (localTerrain[targetLoc[1]][targetLoc[2]].items[i].itemtype == "empty") then
			localTerrain[targetLoc[1]][targetLoc[2]].items[i] = deepcopy(targetItem)
			return true
		end
	end

	return false
end







